package com.lylg.picture_share.controller;

import com.github.pagehelper.PageInfo;
import com.lylg.picture_share.entity.Manager;
import com.lylg.picture_share.service.impl.ManagerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
    @RequestMapping("/manager")
    public class
ManagerController {
      private PageInfo<Manager> pageInfo;

        @Autowired
        private ManagerServiceImpl managerService;
        @GetMapping("/toLogin")
        public String toLogin(){
            return "/manager/managerlogin";
        }
        @GetMapping("/toindex")
        public String toSuccess(){
            return "/manager/index";
        }
        @GetMapping("/tomanager")
        public String toManager(){
        return "/manager/manager";
    }
        @GetMapping("/tomanager3")
       public String toManager1(){
        return "/manager/manager3";
    }
        @GetMapping("/toupdpwd")
        public String toUpdpwd(){
        return "/manager/updpwd";
    }
        @PostMapping("/login")
        public String login(String manager_name, String manager_pwd, HttpSession session){
            Manager manager = managerService.selectManagerByNP(manager_name, manager_pwd);
            if(manager!=null){
                session.setAttribute("loginmanager",manager);
                return "redirect:/manager/toindex";
            }
            else {
                return "redirect:/manager/toLogin";
            }
        }
        @RequestMapping("/exit")
        public String doExit(HttpSession session){
            //从session中移除
            session.removeAttribute("loginmanager");
            return "redirect:/manager/toLogin";

        }

        //manager：会将查询条件按照名称一致的原则（表单控件的名称和实体类属性的名称）进行自动封装
        //currentPage参数名称要和url传值的参数名称保持一致
        @RequestMapping("/listui")
        public String doList(Map<String,Object> model, Manager queryBean, Integer currentPage){

            //因为页面首次加载的时候传递不过来页码信息
            if(currentPage==null){

                currentPage=1;
            }
            else{
                //防止点击上一页越界
                if(currentPage==0){
                    currentPage=1;
                }

            }

            Integer pageSize=2;
            //根据查询条件查询数据
//            PageInfo<Manager>
                    pageInfo=managerService.getManagers(queryBean,currentPage,pageSize);


            //pageInfo类型对象中封装了分页相关的所有信息（getList）
            //有一个是什么样的方法可以得到查询到的第一页的数据（List）
            //将数据放到模型中
            model.put("qb",queryBean);//将查询条件也放到数据模型中
            //将pageInfo整个对象转过去
            model.put("pb",pageInfo);

            //会去thymeleaf模板所处的位置找对应的文件
            return "/manager/manager3";

        }
//        @RequestMapping("/list")
//        public String getList(Manager manager,Model model){
//            List<Manager> managers = managerService.selectAllManagers(manager);
//            model.addAttribute("qb",managers);
//            return "/manager/manager3";
//        }
//    @RequestMapping("/updmanagerpwdui")
//    public String doUpdPwdUI(){
//
//        return "/manager/updpwd";
//
//    }


    @RequestMapping("/updpwd")
    public String doUpdPwd(String manager_pwd,HttpSession session){

        Integer managerId=0;
        if(session.getAttribute("loginmanager")!=null){
            managerId=((Manager)session.getAttribute("loginmanager")).getManager_id();
        }

        //修改密码
        managerService.updPwd(managerId,manager_pwd);

        //清除当前的登录状态，让用户重新登录
        session.removeAttribute("loginmanager");
        return "redirect:/manager/toLogin";

    }

    @RequestMapping("/updstatus")
    public String doUpdStatus(Integer manager_id){

        managerService.updateStatus(manager_id);

        return "redirect:/manager/listui";

    }


    @RequestMapping("/addui")
    public String addUI(){


        return "/manager1/add.html";

    }


    //manager中只封装了名称和角色
    @RequestMapping("/save")
    public String doSave(Manager manager){
        manager.setManager_status("正常");
        manager.setManager_pwd("123456");
        managerService.saveManager(manager);
        return "redirect:/manager/listui";
    }
    @RequestMapping("/restpwd")
    public String doRestPwd(String manager_pwd,HttpSession session){

        Integer managerId=0;
        if(session.getAttribute("loginmanager")!=null){
            managerId=((Manager)session.getAttribute("loginmanager")).getManager_id();
        }
        //初始化密码
        manager_pwd="123456";

        //重置密码
        managerService.updPwd(managerId,manager_pwd);

        //清除当前的登录状态，让用户重新登录
        session.removeAttribute("loginmanager");
        return "redirect:/manager/toLogin";


    }

}





